
package com.kanjian.music.menu;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.adapter.MusicMenuListAdapater;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.database.DatabaseManager;
import com.kanjian.music.database.MusicDao;
import com.kanjian.music.dialog.ShareDialog;
import com.kanjian.music.download.DownloadManager;
import com.kanjian.music.download.MusicDownloadTask;
import com.kanjian.music.entity.MenuContent;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.User;
import com.kanjian.music.network.SimpleHttpRequestUtil;
import com.kanjian.music.util.PlayerUtil;

public class MusicMenuLayout extends FrameLayout {

    private Music mMusic;

    private TextView mTv_musicName;

    private ListView mMenuListView;

    private MusicMenuListAdapater mListAdapater;

    private BaseActivity mActivity;

    private LocalBroadcastManager mLoaclBroadcastManager;

    public MusicMenuLayout(Context context) {
        super(context);
    }

    public MusicMenuLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mActivity = (BaseActivity) context;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_music_menu, null);
        this.addView(view);
        this.mTv_musicName = (TextView) view.findViewById(R.id.music_menu_music_name);
        this.mMenuListView = (ListView) view.findViewById(R.id.music_menu_list);
        this.mLoaclBroadcastManager = LocalBroadcastManager.getInstance(mActivity);
    }

    public void setMusic(Music music) {
        this.mMusic = music;
        mTv_musicName.setText(mMusic.getMusicName());
        ArrayList<MenuContent> list = new ArrayList<MenuContent>();
        if (!(mMusic.getIsFav() == 0)) {
            list.add(new MenuContent(0, "喜欢"));
        } else {
            list.add(new MenuContent(0, "取消喜欢"));
        }
        list.add(new MenuContent(0, "分享"));
        list.add(new MenuContent(0, "缓存至本地"));
        list.add(new MenuContent(0, "添加至播放器"));
        list.add(new MenuContent(0, "查看音乐人"));
        list.add(new MenuContent(0, "bump相似歌曲"));
        mListAdapater = new MusicMenuListAdapater(mActivity, list);
        mMenuListView.setAdapter(mListAdapater);
        mMenuListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                processMenuClick(position);
            }
        });
    }

    private void processMenuClick(int position) {
        switch (position) {
            case 0:
                // 喜欢
                if (mMusic.getIsFav() == 0) {
                    mMusic.setIsFav(1);
                } else {
                    mMusic.setIsFav(0);
                }
                MusicDao dao = DatabaseManager.session.getMusicDao();
                dao.insertOrReplace(mMusic);
                Intent intent = new Intent();
                intent.setAction(IntentConstants.ACTION_ON_LIKESTATUS_CHANGED);
                mLoaclBroadcastManager.sendBroadcast(intent);
                if (User.isLogin()) {
                    // 同步服务器
                    SimpleHttpRequestUtil.doLike(mMusic.getMusicId(), mMusic.getIsFav() == 1);
                }
                break;
            case 1:
                // 分享
                ShareDialog.newInstance(mMusic).show(mActivity.getSupportFragmentManager(), "");
                break;
            case 2:
                // 缓存至本地
                MusicDownloadTask t = new MusicDownloadTask(mMusic, 1, true);
                DownloadManager.addTask(t);
                break;
            case 3:
                PlayerUtil.addMusic(mMusic);
                break;
            case 4:
                // 查看音乐人
                Intent musicianIntent = new Intent();
                musicianIntent.setClass(mActivity, MusicianHomeActivity.class);
                musicianIntent.putExtra(IntentConstants.MUSICIANID, mMusic.getUserId());
                mActivity.startActivityWithAnimation(musicianIntent);
                break;
            case 5:
                break;
        }
        GoneOrShow();
    }

    public void GoneOrShow() {
        if (this.isShown()) {
            this.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.anim_menu_gone));
            this.setVisibility(View.GONE);
        } else {
            this.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.anim_menu_show));
            this.setVisibility(View.VISIBLE);
        }
    }
}
